﻿using System;
using System.Collections.Generic;
using System.Text;
using ShootSearch.Core;
using System.Data;
using Quartz;
using ShootSearch.FileSystem;
using ShootSearch.Indexers;

namespace ShootSearch.Parsers
{
    public class Processor: ISpiderMan
    {
        protected IFileSystem inputFile;
        protected IIndexer outputIndexer;

        protected IPageParser pageParser;
        protected IFilter binaryParser;

        protected Dictionary<string, List<IFilter>> filters;

        protected ProcessorConfig config;   

        public Processor()
        {
            config = new ProcessorConfig();
        }

        #region ISpiderMan Members

        public Quartz.JobDetail Next { get; set; }
        public JobStatus Status { get; protected set; }
        public DataTable DetailStatus { get; protected set; }

        #endregion

        #region IInterruptableJob Members

        public void Interrupt()
        {
            if(Status == JobStatus.Running)
                Status = JobStatus.Stopping;
        }

        #endregion

        #region IJob Members

        public void Execute(Quartz.JobExecutionContext context)
        {
            if (Status == JobStatus.Unstarted) Status = JobStatus.Running;

            Config = context.MergedJobDataMap[Task.JOB_CONFIG] as ProcessorConfig;
            if (Config != null)
            {
                DoProcess();
            }

            System.Diagnostics.Debug.WriteLine(context.JobDetail.Name + " Completed!");

            if (Next != null)
            {
                context.Scheduler.ScheduleJob(Next, new SimpleTrigger());
            }
        }

        protected bool OpenInputOutput()
        { 
            if(config ==null) return false;



            filters = new Dictionary<string, List<IFilter>>();
            foreach (AssemblyWithConfig filter in config.Filters)
            {
                IFilter ifilter = filter.GetObjectWithConfig() as IFilter;
                if (ifilter != null)
                {
                    List<string> list = ifilter.ContentType;

                    foreach (string contentType in list)
                    {
                        List<IFilter> filterList = new List<IFilter>();
                        if (filters.ContainsKey(contentType))
                        {
                            filterList = filters[contentType];
                        }
                        filterList.Add(ifilter);
                        filters[contentType] = filterList;
                    }
                }
            }



            return true;
        }

        protected void CloseInputOutput()
        { 
            
        }

        protected void DoProcess()
        {
            bool ready = OpenInputOutput();
            if (ready)
            {
                if (string.IsNullOrEmpty(config.ID))
                {
                    List<string> ids = inputFile.ListAllFSIDs();
                    for (int i = 0; i < ids.Count; i++)
                    {

                        ProcessFSID(ids[i]);
                    }
                }
                else ProcessFSID(config.ID);
                
            }

            CloseInputOutput();
        }

        protected void PrepareFSID(string id)
        {
            //outputIndexer.Config
            outputIndexer.Create();
        }


        protected void ProcessFSID(string id)
        {
            inputFile.FSID = id;
            List<string> urls = inputFile.ListFileNames();

            for (int i = 0; i < urls.Count; i++)
            {
                object doc = pageParser.ParsePage(urls[i], inputFile.ReadText(urls[i]));
                outputIndexer.Add(doc);
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            
        }

        #endregion

        #region IConfigable Members

        public SpiderManConfig Config
        {
            get
            {
                return config;
            }
            set
            {
                if (value is ProcessorConfig)
                {
                    config = value as ProcessorConfig;
                }
            }
        }

        #endregion
    }
}
